<? include ("../init.php") ?>
<?
  $title = $_POST['title'];
  $status = $_POST['status'];
  $detail = $_POST['detail'];
  $time = time();
  

  $sql = "INSERT INTO promotion(promo_title,promo_adddate,promo_detail,promo_active) VALUES('$title',$time,'$detail',$status)";

  $db->execute($sql);
  
  $aid = $db->lastInsertedId();
  
  // ---------- UPLOADS Image ----------
  $handle = new Upload($_FILES['image']);
  
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->image_resize = true;
        $handle->file_name_body_pre = $aid.'_';
        //$handle->image_ratio_fill = true;
        $handle->image_x = 360;
        $handle->image_ratio_y = true;
        $handle->Process(APPPATH.'/images/promotions');
        
        if ($handle->processed)
        {
           $sql = "UPDATE promotion SET promo_img='$handle->file_dst_name' WHERE promo_id=$aid";
           if(!mysql_query($sql))
           {
                $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
                unlink(APPPATH."/images/promotions/".$handle->file_dst_name);
           }
           else
           {
            $handle->image_resize = true;
            $handle->file_name_body_pre = $aid.'_thumb_';
            $handle->image_ratio_fill = true;
            $handle->image_x = 180;
            $handle->image_y = 180;
            $handle->Process(APPPATH.'/images/promotions');
           }
        }
        else
        {
            $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
        }
        
        $handle->clean();
    }
    else
    {
        $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
    }
  }
  
  //$sql = "UPDATE product SET product_img='$name' WHERE product_id=$aid";
  //$db->execute($sql);
  
  header("Location:index.php");
?>